package com.monitorchart;

import java.text.DecimalFormat;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TableLayout.LayoutParams;

import com.monitorchart.data.DataFromJson;
import com.monitorchart.data.JsonStr;
import com.monitorchart.data.TimeStyle;

public class SysMonitorActivity extends Activity
{
	/**
	 * 图表数据
	 */
	private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
	
	/**
	 * 图表格式
	 */
	private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
	
	/**
	 * Spnner业务选择栏
	 */
	private Spinner mSp;
	
	/**
	 * 图表绘制区
	 */
	private LinearLayout mLayout;
	
	/**
	 * 图表
	 */
	private GraphicalView mChartView;
	
	private double yMax;
	
	/**
	 * y坐标单位
	 */
	private String numStr;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_monitor);
		mSp = (Spinner) findViewById(R.id.mSpinner);
		mLayout = (LinearLayout) findViewById(R.id.mLayout);
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.systemMonitor,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSp.setAdapter(adapter);
		mSp.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
			{
				mChartView.repaint();
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> parent)
			{
				
			}
		});
		setRenderer(yMax + 5, TimeStyle.HOURTIME, 1, numStr);
		DataFromJson mData = new DataFromJson();
		setDataset(mSp.getSelectedItem().toString(), mData.dealJsonData(JsonStr.dayStr, "1to12"));
		mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
		mLayout.setBackgroundColor(Color.WHITE);
		mLayout.addView(mChartView, new LayoutParams().FILL_PARENT, new LayoutParams().FILL_PARENT);
	}
	
	/**
	 * 设置图表数据
	 * 
	 * @param title 图表的标题
	 * @param mdata 图表的Y坐标
	 */
	public void setDataset(String title, double[] mdata)
	{
		XYSeries mSeries = new XYSeries(title);
		for (int i = 0; i < 12; i++)
		{
			yMax = yMax > mdata[i] ? yMax : mdata[i];
		}
		
		numStr = unitFormat(yMax);
		
		for (int i = 0; i < mdata.length; i++)
		{
			mdata[i] = mdata[i] / TimeStyle.UNITTYPE;
			mSeries.add(i, mdata[i]);
		}
		
		mDataset.addSeries(mSeries);
	}
	
	/**
	 * 将次数转换为对应单位的数
	 * 
	 * @param yMax
	 */
	public String unitFormat(double yMax)
	{
		String unitStr = "";
		if (yMax >= TimeStyle.HundredTHOUSAND)
		{
			yMax = yMax / TimeStyle.HundredTHOUSAND;
			TimeStyle.UNITTYPE = TimeStyle.HundredTHOUSAND;
			unitStr = "十万次";
		}
		else if (yMax > TimeStyle.TenTHOUSAND && yMax < TimeStyle.HundredTHOUSAND)
		{
			yMax = yMax / TimeStyle.TenTHOUSAND;
			TimeStyle.UNITTYPE = TimeStyle.TenTHOUSAND;
			unitStr = "万次";
		}
		else if (yMax > TimeStyle.THOUSAND && yMax < TimeStyle.TenTHOUSAND)
		{
			yMax = yMax / TimeStyle.THOUSAND;
			TimeStyle.UNITTYPE = TimeStyle.THOUSAND;
			unitStr = "千次";
		}
		else if (yMax > TimeStyle.HUNDRED && yMax < TimeStyle.THOUSAND)
		{
			yMax = yMax / TimeStyle.HUNDRED;
			TimeStyle.UNITTYPE = TimeStyle.HUNDRED;
			unitStr = "百次";
		}
		else if (yMax > TimeStyle.TEN && yMax < TimeStyle.HUNDRED)
		{
			yMax = yMax / TimeStyle.TEN;
			TimeStyle.UNITTYPE = TimeStyle.TEN;
			unitStr = "十次";
		}
		else
		{
			TimeStyle.UNITTYPE = TimeStyle.UNIT;
			unitStr = "次";
		}
		
		DecimalFormat df = new DecimalFormat("0.00");
		
		yMax = Double.parseDouble(df.format(yMax));
		
		return unitStr;
	}
	
	/**
	 * 设置mRenderer
	 * 
	 * @param yMax y轴最大值
	 * @param timeStr 时间格式：月、日、小时、分钟
	 * @param scale 缩放比列
	 * @param chartTitle 图表名
	 * @param numStr 数量单位：十万次、万次、千次、百次、十次、次
	 */
	public void setRenderer(double yMax, String timeStr, int scale, String numStr)
	{
		mRenderer.setYTitle("总量(" + numStr + ")");
		mRenderer.setXTitle("时间(" + timeStr + ")");
		mRenderer.setYAxisMax(yMax);
		mRenderer.setAxesColor(Color.YELLOW);
		mRenderer.setAxisTitleTextSize(16);
		mRenderer.setPointSize(10);
		mRenderer.setLabelsTextSize(15);
		mRenderer.setLegendTextSize(15);
		mRenderer.setBackgroundColor(Color.WHITE);
		mRenderer.setGridColor(Color.YELLOW);
		mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
		
		XYSeriesRenderer renderer = new XYSeriesRenderer();
		renderer.setPointStyle(PointStyle.CIRCLE);
		renderer.setFillPoints(true);
		mRenderer.addSeriesRenderer(renderer);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
}
